<template>
  <div>
    <!-- 头部 -->
    <van-nav-bar @click-left="$router.push('/articleDetail')" title="精彩评论" left-arrow></van-nav-bar>
    <!-- 评论列表 -->
    <div class="item" v-for="item in commentList" :key="item.id">
      <div class="head">
        <img :src="item.user.head_img" alt />
        <div>
          <p>{{ item.user.nickname }}</p>
          <span>{{ item.created_date }}</span>
        </div>
        <span @click="reqlyFn(item.id)">回复</span>
      </div>
      <!-- 嵌套评论列表 -->
      <div class="commentItem" v-if="item.parent">
        <div class="top">
          <div class="left">
            <span>{{ item.parent.user.nickname }}</span> &nbsp;&nbsp;&nbsp;
            <span>2分钟前</span>
          </div>
          <span>回复</span>
        </div>
        <commentItem @sendData='reqlyFn' v-if="item.parent" :obj="item.parent"></commentItem>
        <div class="bottom">{{ item.parent.content }}</div>
      </div>
      <div class="text">{{ item.content }}</div>
    </div>
    <!-- 底部 -->
    <commentFooter ref="footer" @update="updateFn" :article="article"></commentFooter>
  </div>
</template>


<script>
import commentFooter from "@/components/commentFooter.vue";
import { getArticleDetail, post_comment } from "@/api/user";
import commentItem from "@/components/commentltem.vue";
export default {
  components: { commentFooter, commentItem },
  data() {
    return {
      article: {},
      commentList: {},
    //   updateFn:{}
    };
  },
  created() {
    // 在评论列表接收到文章id
    console.log(this.$route.query.id);
    // 获取位置详情
    getArticleDetail(this.$route.query.id).then((res) => {
      console.log(39, res);
      this.article = res.data.data;
    });
    post_comment(this.$route.query.id).then((res) => {
      console.log(43, res);
      this.commentList = res.data.data;
    });
  },
  methods: {
    //  获取评论列表
    updateFn() {
      post_comment(this.$route.query.id).then((res) => {
        this.commentList = res.data.data;
      });
    },
    reqlyFn(id) {
      /* 在事件处理函数当中，通过ref属性获取底部评论块组然后调用focusFn函数
         显示底部多行文本域并向子组件传评论id*/
      console.log(id);
      this.$refs.footer.focusFn();
      this.$refs.footer.parent_id = id
    },
  },
};
</script>


<style lang="less" scoped>
.van-nav-bar {
  border-bottom: 2px solid #ddd;
}
.item {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    > img {
      width: (50/360) * 100vw;
      height: (50/360) * 100vw;
      display: block;
      border-radius: 50%;
    }
    > div {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      > span {
        font-size: 12px;
        color: #999;
        line-height: 25px;
      }
    }
    > span {
      color: #999;
      font-size: 13px;
    }
  }
  .text {
    font-size: 14px;
    color: #333;
    padding: 20px 0 10px 0;
  }
  .commentItem {
    border: 1px solid #ccc;
    padding: 5px;
    margin-top: 10px;
    .top {
      font-size: 12px;
      color: #aaa;
      display: flex;
      justify-content: space-between;
    }
    .bottom {
      font-size: 13px;
      line-height: 40px;
    }
  }
}
</style>